

import 'package:syria_sports/data/models/error_model.dart';

import '../../core/utils/pref_utils.dart';
import '../../core/utils/show_toast.dart';
import '../apiClient/api_client.dart';
import '../apiClient/api_keys.dart';
import '../models/data.dart';
import '../remote/dio_helper.dart';

class CPController{
  static Future<Data?> getHomeData()async{
    var res = await DioHelper.getData(
        url: ApiClient.home, bearerToken: PrefUtils.getToken(),
        query: {
          /*if(!kIsWeb)
          if(Platform.isAndroid || Platform.isIOS)
      "device_id" :await _getId().catchError((e)=>"error"),*/
    });

    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
      return null;
    }else{

    }
    Data response = Data.fromJson(res.data);
    return response;
  }

  static Future<Data?> selectExercise(String exerciseId)async{
    var res = await DioHelper.postData(
      url: ApiClient.home,
      data: {
        'exercise_id':exerciseId,
      },
      bearerToken: PrefUtils.getToken(),
    );
    if(res == null)return null;
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else{
      showMessage(res.data[ApiKeys.message]);
    }
    return null;
  }

  static Future<Data?> addAttendance(String barcode , String? exerciseId)async{
    var res = await DioHelper.postData(
      url: ApiClient.attendance,
      data: {
        'exercise_id':exerciseId,
        'barcode' : barcode,
        'datetime': DateTime.now().toIso8601String(),
      },
      bearerToken: PrefUtils.getToken(),
    );
    if(res == null)return null;
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else{
      showMessage(res.data[ApiKeys.message]);
    }
    return null;
  }
  static Future<Data?> getPublicHomeData()async{
    var res = await DioHelper.getData( url: ApiClient.publicHome);

    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
      return null;
    }else{

    }

    Data response = Data.fromJson(res.data);
    return response;
  }


}
/*
Future<String?> _getId() async {
  var deviceInfo = DeviceInfoPlugin();
  if (Platform.isIOS) { // import 'dart:io'
    var iosDeviceInfo = await deviceInfo.iosInfo;
    return iosDeviceInfo.identifierForVendor; // unique ID on iOS
  } else if(Platform.isAndroid) {
    var androidDeviceInfo = await deviceInfo.androidInfo;
    return androidDeviceInfo.id; // unique ID on Android
  }else{
    return 'pc';
  }
}*/
